package main

import (
	"log"
	"time"
	"yuezhang/async"
)

func main() {
	log.Println("main thread start")
	future := async.Exec[int](f1, 123, "worker accomplished")
	log.Println("call the worker......")

	f2(2, "main accomplished") // do something ......

	log.Println("waiting for worker......")
	value := future.Await()
	log.Println("worker returned", value)
	log.Println("main thread exit")
}

// in Go way......
func usingChan() {
	ch := make(chan struct{})
	go func() {
		defer close(ch)
		f3()
		ch <- struct{}{}
	}()
	<-ch
}

func f1(n int, str string) int {
	log.Println("\tinside worker")
	time.Sleep(time.Second * 5)
	log.Printf("\tcost %ds, %s\n", 5, str)
	return n
}

func f2(n int, str string) {
	log.Println("main is doing something......")
	time.Sleep(time.Second * time.Duration(n))
	log.Printf("cost %ds, %s\n", n, str)
}

func f3() {
	time.Sleep(time.Second * 5)
}
